package com.lgc.javathread.locktest.synchronizdAndLock.synchronizedTest;

import java.util.concurrent.TimeUnit;

public class Program2 {
    int count = 0;

    public static void main(String[] args) {
        Program2 program2 = new Program2();

        // 写法1
        /*new Thread(() -> {
            program2.m();
        }).start();*/

        // 写法2
        //new Thread(program2::m,"t1").start();

        // 写法3
        Runnable r=new Runnable() {
            @Override
            public void run() {
                program2.m();
            }
        };
        new Thread(r,"tt1").start();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            program2.m();
        }).start();
    }

    public synchronized void m() {
        System.out.println(Thread.currentThread().getName() + " start");
        while (true) {
            count++;
            System.out.println(Thread.currentThread().getName() + " count= " + count);

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (count == 5) {
                int i = 1 / 0;
                System.out.println(i);
            }

            if (count == 20) {
                break;
            }
        }
    }
}
